<?php
// +---------------------------------------------------------------------+
// | ALINEJUN    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/ALINEJUN                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

/**
 * 风景逻辑
 */
class Scenery extends LogicBase
{
    
    /**
     * 风景分类编辑
     */
    public function sceneryCategoryEdit($data = [])
    {
        
        $validate_result = $this->validateSceneryCategory->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateSceneryCategory->getError()];
        }
        
        $url = url('sceneryCategoryList');
        
        $result = $this->modelSceneryCategory->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '风景分类' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelSceneryCategory->getError()];
    }
    
    /**
     * 获取风景列表
     */
    public function getSceneryList($where = [], $field = 'a.*,m.nickname,c.name as category_name', $order = '')
    {
        
        $this->modelScenery->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                    [SYS_DB_PREFIX . 'scenery_category c', 'a.category_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelScenery->join = $join;
        
        return $this->modelScenery->getList($where, $field, $order);
    }
    
    /**
     * 获取风景列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * 风景信息编辑
     */
    public function sceneryEdit($data = [])
    {
        
        $validate_result = $this->validateScenery->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateScenery->getError()];
        }
        
        $url = url('sceneryList');
        
        empty($data['id']) && $data['member_id'] = MEMBER_ID;
        
        $data['content'] = html_entity_decode($data['content']);
        
        $result = $this->modelScenery->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '风景' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '风景操作成功', $url] : [RESULT_ERROR, $this->modelScenery->getError()];
    }

    /**
     * 获取风景信息
     */
    public function getSceneryInfo($where = [], $field = 'a.*,m.nickname,c.name as category_name')
    {
        if(isset($where['id'])){
            $where['a.id'] = $where['id'];
            unset($where['id']);
        }
        $this->modelScenery->alias('a');
        
        $join = [
                    [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                    [SYS_DB_PREFIX . 'scenery_category c', 'a.category_id = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $this->modelScenery->join = $join;
        
        return $this->modelScenery->getInfo($where, $field);
    }
    
    /**
     * 获取分类信息
     */
    public function getSceneryCategoryInfo($where = [], $field = true)
    {
        
        return $this->modelSceneryCategory->getInfo($where, $field);
    }
    
    /**
     * 获取风景分类列表
     */
    public function getSceneryCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelSceneryCategory->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 风景分类删除
     */
    public function sceneryCategoryDel($where = [])
    {
        
        $result = $this->modelSceneryCategory->deleteInfo($where);
        
        $result && action_log('删除', '风景分类删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '风景分类删除成功'] : [RESULT_ERROR, $this->modelSceneryCategory->getError()];
    }
    
    /**
     * 风景删除
     */
    public function sceneryDel($where = [])
    {
        
        $result = $this->modelScenery->deleteInfo($where);
        
        $result && action_log('删除', '风景删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '风景删除成功'] : [RESULT_ERROR, $this->modelScenery->getError()];
    }

    public function getSceneryType($where)
    {
        $result = db('scenery')->field('category_id')->where($where)->group('category_id')->select();
        $category_info = db('scenery_category')->field('id,name')->where(['id'=>['in',array_column($result,'category_id')]])->select();
        return $category_info;
    }
}
